package cn.ujn.medicalbackend.service.impl;

import cn.ujn.medicalbackend.common.ErrorCode;
import cn.ujn.medicalbackend.domain.Material;
import cn.ujn.medicalbackend.exception.BusinessException;
import cn.ujn.medicalbackend.mapper.MaterialMapper;
import cn.ujn.medicalbackend.request.MaterialAddRequest;
import cn.ujn.medicalbackend.request.MaterialDeleteRequest;
import cn.ujn.medicalbackend.request.MaterialUpdateRequest;
import cn.ujn.medicalbackend.service.MaterialService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author 15926
 * @description 针对表【material】的数据库操作Service实现
 * @createDate 2024-10-26 15:55:04
 */
@Service
public class MaterialServiceImpl extends ServiceImpl<MaterialMapper, Material>
        implements MaterialService {
    @Override
    public Page<Material> materialPageTransfer(Page<Material> tempPage) {
        return tempPage;
    }

    @Override
    public long addMaterial(MaterialAddRequest materialAddRequest, HttpServletRequest request) {
        Material material = new Material();
        BeanUtils.copyProperties(materialAddRequest, material);
        System.out.println(material.toString());
        Date now = new Date();
        material.setCreateTime(now);
        material.setUpdateTime(now);
        // 3.校验信息
        if (material.getTitle() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "不能为空");
        }
        boolean result;
        result = save(material);
        if (!result) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "保存失败");
        }

        return material.getId();
    }

    @Override
    public boolean updateMaterial(MaterialUpdateRequest materialUpdateRequest, HttpServletRequest request) {
        if (materialUpdateRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Date now = new Date();
        Material material = getById(materialUpdateRequest.getId());
        if (material == null) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "该销售地不存在，更新失败");
        }
        BeanUtils.copyProperties(materialUpdateRequest, material);
        material.setUpdateTime(now);

        boolean result = updateById(material);
        if (!result) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "更新失败");
        }

        return true;
    }

    @Override
    public boolean deleteMaterial(MaterialDeleteRequest materialDeleteRequest, HttpServletRequest request) {
        // 鉴权，只有管理员可创建
        Integer materialId = materialDeleteRequest.getId();

        if (materialId == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "；医生id和账户名不能为空");
        }
        QueryWrapper<Material> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", materialId);
        long count = this.count(queryWrapper);
        if (count <= 0) {
            throw new BusinessException(ErrorCode.NULL_ERROR, "没有该药品");
        }

        boolean result = removeById(materialId);
        if (!result) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "删除失败");
        }
        return true;
    }
}




